package com.gec.web;

import com.gec.domain.Dept;
import com.gec.service.DeptService;
import com.gec.service.impl.DeptServiceImpl;
import com.gec.utils.CrossRegion;
import com.gec.utils.PageModel;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = {"/findAllDept", "/saveDept", "/updateDept", "/deleteDept", "/findDeptById","/findDept"})
public class DeptController extends HttpServlet {
    //service对象
    DeptService service = new DeptServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        //跨域设置
        CrossRegion.setCross(response);
        //如果是get,设置200ok
        if ("OPTIONS".equals(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
            return;
        }
        String servletPath = request.getServletPath();


        if (servletPath.equals("/findAllDept")) {
            findalldept(request,response);
        } else if (servletPath.equals("/saveDept") || servletPath.equals("/updateDept")) {
            saveorupdateDept(request,response);
        } else if (servletPath.equals("/deleteDept")) {
            deletedept(request,response);
        } else if (servletPath.equals("/findDeptById")) {
            findByiddept(request,response);
        }else if(servletPath.equals("/findDept")){
            finddept(request,response);
        }
    }

    /**
     * 添加修改
     * */
    private void saveorupdateDept(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");

        String name = request.getParameter("name");
        String remark = request.getParameter("remark");

        Dept dept = new Dept();
        dept.setName(name);
        dept.setRemark(remark);

        //如果id为0，则添加
        if (id.equals("0")) {
            service.addDept(dept);
        } else {//反之则修改
            int uid = Integer.parseInt(id);
            dept.setId(uid);
            service.updateDept(dept);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("message", "success");

        Gson gson = new Gson();
        String json = gson.toJson(map);
        response.getWriter().write(json);
    }

    /**
     * 删除
     * */
    private void deletedept(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        service.deleteDept(Integer.parseInt(id));

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);

        Gson gson = new Gson();
        String json = gson.toJson(map);
        response.getWriter().write(json);
    }

    /**
     * 查询全部
     * */
    private void finddept(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //用户列表：需要发送给前端的
        List<Dept> depts = null;

        Dept dept = new Dept();
        //数据库查询全部的结果
        depts = service.findDept(dept);

        Map<String, Object> map = new HashMap<>();
        map.put("depts", depts);
        //使用json返回
        Gson gson = new Gson();
        String s = gson.toJson(map);
        //响应前台
        response.getWriter().write(s);
    }

    /**
     * 查询全部，添加了分页查询
     * */
    private void findalldept(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得当前页码
        String pageIndex = request.getParameter("pageIndex");
        int page = !"".equals(pageIndex) && pageIndex != null ? Integer.parseInt(pageIndex) : 1;
        //设置到PageModel
        PageModel model = new PageModel();
        model.setPageIndex(page);

        //用户列表：需要发送给前端的
        List<Dept> depts = null;

        Dept dept = new Dept();
        //获取参数：用户名
        String name = request.getParameter("name");
        if (name != null) {
            System.out.println("name = " + name);
            dept.setName(name);
        }
        //从数据库获取总记录数
        int count = service.getTotalDeptCount(dept);
        //将总记录数设置到分页
        model.setTotalRecoreSum(count);
        //数库查询全部的结果
        depts = service.findAllDept(dept, model);

        Map<String, Object> map = new HashMap<>();
        map.put("model", model);
        map.put("depts", depts);
        //使用json返回
        Gson gson = new Gson();
        String s = gson.toJson(map);
        //响应前台
        response.getWriter().write(s);
    }

    /**
     * 根据id查询
     * */
    private void findByiddept(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //从页面获取id
        //int id=Integer.parseInt(req.getParameter("id"));
        // System.out.println(id);
        //返回到数据库
        Dept depts = service.findDeptById(Integer.parseInt(request.getParameter("id")));

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("depts", depts);

        Gson gson = new Gson();
        String json = gson.toJson(map);

        response.getWriter().write(json);
    }

}
